package Classes.GameViews// ActionScript file
{
	import Classes.GameDatas.BuildType;
	import Classes.GameDatas.GD_Building;
	import Classes.GameDatas.GD_Building_Func;
	import Classes.GameDatas.GD_Map;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameErrors.GameError;
	import Classes.GameUtils.GU_HitTest;
	import Classes.GameUtils.GU_IsoUtils;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameUtils.GU_dealPower;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building_Func;
	import Classes.GameViews.ViewDisplayers.VD_MapGrid;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.GameViews.ViewPanels.VP_errorPanel;
	import Classes.Managers.M_Languages;
	import Classes.staticinfo.Language;
	
	import MV_BMP.MovieClip_BMP;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class GV_BuildingSetLayer extends GameView
	{
		private var _setBuilding:MT_Building_Func;/*设置的建筑*/
		private var _setGrid:GD_MapGrid;
		private var _relevanceGridsAry:Array;
		private var _layerBuilding:Sprite;
		private var _layerGrids:Sprite;
		public function GV_BuildingSetLayer():void
		{
			this.init();
		}
		
		private function init():void
		{
			this._layerGrids=new Sprite;
			this.addChild(this.layerGrids);
			this._layerBuilding=new Sprite;
			this.addChild(this.layerBuilding);
			this.layerBuilding.alpha=0.5;
			this.visible=false;
			this.addListener();
		}
		
		public function creat(width:Number,height:Number):void
		{
			this.graphics.beginFill(0x000000,0.1);
			this.graphics.drawRect(-width/2,0,width,height);
			this.graphics.endFill();
		}
		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandle);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandle);
		}
		
		private function mouseMoveHandle(e:MouseEvent):void
		{
			this.resetLayerGrids();
			if(this.setBuilding)
			{
				var positionPoint:Point=new Point(this.mouseX,this.mouseY);
				var isoPoint:Point=GU_IsoUtils.screenToIso(positionPoint);
				var atx:int=isoPoint.x/GV_Map.instance.gridSize;
				var aty:int=isoPoint.y/GV_Map.instance.gridSize;
				var positionX:int=atx-int(this.setBuilding.size/2);
				var positionY:int=aty-int(this.setBuilding.size/2);
				try
				{
				    this._setGrid=GV_Map.instance.getCoordinateGrid(new Point(positionX,positionY));
					this._relevanceGridsAry=this.setBuilding.getRelevanceGridsAry(this.setGrid);
					for each(var grid:GD_MapGrid in this.relevanceGridsAry)
					{
						var view:VD_MapGrid=grid.view;
						view.fillByEnabled();
						this.layerGrids.addChild(view);
					}
					this.setBuilding.x=this.setGrid.view.x;
					this.setBuilding.y=this.setGrid.view.y;

					//两个建筑重叠，设为半透明
					for each(var gb:GD_Building_Func in GV_Map.instance.mapData.buildingsObj)
					{
						var checkTarget:MovieClip_BMP=(gb.view.img as Sprite).getChildAt(0) as MovieClip_BMP;
						var checkTarget1:MovieClip_BMP=(this.setBuilding.img as Sprite).getChildAt(0) as MovieClip_BMP;
						
						if(checkTarget==null || checkTarget1==null){gb.view.setTransparent(false);return ;}
						if(GU_HitTest.hitObj(checkTarget,checkTarget1,this.stage))
							gb.view.setTransparent(true);
						else
							gb.view.setTransparent(false);
					}
				}
				catch(e:GameError)
				{
					
				}
				/*var newPositionPoint:Point=GL_IsoUtils.isoToScreen(new Point(this.positionX*GV_Map.instance.gridSize,this.positionY*GV_Map.instance.gridSize));
				this.setBuilding.x=newPositionPoint.x;
				this.setBuilding.y=newPositionPoint.y;
				this.setBuilding.cannotSetSign=!this.setCheck();*/
			}
			if(e)
			{
			    e.updateAfterEvent();
			}
		}
		
		private function resetLayerGrids():void
		{
			while(this.layerGrids.numChildren)this.layerGrids.removeChildAt(0);
		}
		
		private function resetLayerBuilding():void
		{
			while(this.layerBuilding.numChildren)this.layerBuilding.removeChildAt(0);
		}
		
		public function clickSet():void
		{
			if(this.buildEnabled)
			{
				if(setBuilding.buildingFuncData.ztFunType==401 && setBuilding.buildingFuncData.tmpHasBuildInMap)
				{
					VP_errorPanel.instance.setErrorMessage(Language.ownBuild);
					VP_errorPanel.instance.enterFrame();
					GU_Sound.instance.playCommSound("System_Prompt");	
				}else{
					GV_Map.instance.addTarget(this.setBuilding,GV_Map.instance.getCoordinateGrid(new Point(this.setGrid.atx,this.setGrid.aty)));
					//this.setBuilding.buildingData.setBuildType(BuildType.MOVE);
					
					if(setBuilding.buildingFuncData.buildingType==40102001 || setBuilding.buildingFuncData.buildingType==40106001)
					{
						GU_dealPower.instance.sureBuild();
					}
					
					for each(var gb:GD_Building_Func in GV_Map.instance.mapData.buildingsObj)
					gb.view.setTransparent(false);
					this.setOver();
					
					GU_Sound.instance.playCommSound("Button_Building_Down");

				}
				
//				VP_Main.instance.buildReShowHandler(null);
			}
			
		}
		
		public function interuptSet():void
		{
			if(this.setBuilding)
			{
				this.setBuilding.buildingFuncData.setBuildType(BuildType.INVALID);
				this.setOver();
			}
		}
		
		/*private function setCheck():Boolean
		{
			for(var i:int=0;i<this.setBuilding.size;i++)
			{
				for(var j:int=0;j<this.setBuilding.size;j++)
				{
					var checkPoint:Point=new Point(this.positionX+i,this.positionY+j)
					if(!GV_Map.instance.checkPoint(checkPoint))
					{
						return false;
					}
					var grid:GD_MapGrid=GV_Map.instance.getCoordinateGrid(checkPoint);
					if(!grid.isHaven || (grid.building && grid.building!=this.setBuilding) || grid==GV_Map.instance.controlTarget.mapGrid)
					{
						return false;
					}
				}
			}
			return true;
		}*/
		
		public function getBuilding(building:MT_Building_Func):void
		{
			this.visible=true;
			this.setBuilding=building;
			this.layerBuilding.addChild(this.setBuilding);
			this.mouseMoveHandle(null);
		}
		
		public function escSet():void
		{
			
		}
		
		private function setOver():void
		{
			this.reset();
		}
		
		public function reset():void
		{
			this.resetLayerGrids();
			this.resetLayerBuilding();
			this._setBuilding=null;
			this._setGrid=null;
			this._relevanceGridsAry=null;
			this.visible=false;
		}
		
		public function release():void
		{
			this.removeListener();
			if(this.parent)this.parent.removeChild(this);
			while(this.numChildren)this.removeChildAt(0);
		}

		/*getter & setter*/
		public function get setBuilding():MT_Building_Func
		{
			return _setBuilding;
		}

		public function set setBuilding(value:MT_Building_Func):void
		{
			_setBuilding = value;
		}

		public function get setGrid():GD_MapGrid
		{
			return _setGrid;
		}

		public function get relevanceGridsAry():Array
		{
			return _relevanceGridsAry;
		}

		public function get layerBuilding():Sprite
		{
			return _layerBuilding;
		}

		public function get layerGrids():Sprite
		{
			return _layerGrids;
		}
		
		public function get buildEnabled():Boolean
		{
			for each(var grid:GD_MapGrid in this.relevanceGridsAry)
			{
				if(!grid.buildEnabled)return false;
			}
			return true;
		}


	}
}